package org.home.util;

/**
 * Created by nanshouxiao on 2017/7/31 10:54.
 */
public class StringUtils {
    /**
     * 将数组以l某个分隔符拼接成字符串
     *
     * @param delimiter
     * @param elements
     * @return
     */
    public static String join(CharSequence delimiter, Object... elements) {
        if (elements == null) {
            throw new NullPointerException();
        }
        if (delimiter == null) {
            delimiter = "";
        }
        StringBuilder resultStr = new StringBuilder();
        for (Object element : elements) {
            resultStr.append(delimiter).append(element);
        }
        int length = delimiter.length();
        if (resultStr.length() > length) {
            return resultStr.substring(length);
        }
        return resultStr.toString();
    }

    /**
     * 将一个列表的数据用某个分隔符拼接成字符串
     *
     * @param delimiter
     * @param elements
     * @return
     */
    public static String join(CharSequence delimiter, Iterable<?> elements) {
        if (elements == null) {
            throw new NullPointerException();
        }
        if (delimiter == null) {
            delimiter = "";
        }
        StringBuilder resultStr = new StringBuilder();
        for (Object element : elements) {
            resultStr.append(delimiter).append(element);
        }
        int length = delimiter.length();
        if (resultStr.length() > length) {
            return resultStr.substring(length);
        }
        return resultStr.toString();
    }

    public static void main(String[] args) {
    }

}
